package com.yangxd.hospital.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxd.handler.BusinessException;
import com.yangxd.hospital.entity.Department;
import com.yangxd.hospital.mapper.DepartmentMapper;
import com.yangxd.hospital.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yangxd.response.ResultCode;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangxd
 * @since 2021-05-09
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    /**
     * 分页多条件查询科室
     *
     * @param page
     * @param wrapper
     * @return
     */
    @Override
    public IPage<Department> findPageDepartment(Page<Department> page, QueryWrapper<Department> wrapper) {
        return this.baseMapper.findPageDepartment(page,wrapper);
    }

    /**
     * 删除
     *
     * @param departmentId
     */
    @Override
    public void deleteById(Integer departmentId) {
        this.baseMapper.deleteById(departmentId);
    }

    /**
     * 新增科室
     *
     * @param department
     */
    @Override
    public void saveDepartment(Department department) {
        String departmentName = department.getDepartmentName();
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_name",departmentName);
        int count = this.baseMapper.selectCount(queryWrapper);
        if(count > 0) {
            throw new BusinessException(ResultCode.DEPARTMENT_ALREADY_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_ALREADY_EXIST.getMessage());
        }
        department.setDeleted(0);
        this.baseMapper.insert(department);

    }


}
